﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;
using OperGui.Models;

namespace OperGui.ViewModel
{
    public class GroupViewModel : ViewModelBase
    {

        int currentpage;
        string parentid;
        ObservableCollection<CStructureItem> structureItem;

        public GroupViewModel(string ParentId)
        {
            this.parentid = ParentId;
            currentpage = 1;
            structureItem = DataDispacher.Instance.LoadFSCStructureItems("Group");
            
        }

        public List<int> pageNumbers
        {
            get
            {
                List<int> test = new List<int>();
                for (int i = 0; i < structureItem.Count / 12; i++)
                { test.Add(i + 1); }
                if (structureItem.Count % 12 != 0)
                    test.Add(test.Count + 1);
                return test;
            }
        }

        public ICommand switchPage
        {
            get { return new RelayCommand(onPageSwitch) as ICommand; }
        }

        public int Currentpage
        {
            get { return currentpage; }
            set
            {
                currentpage = value;
                base.OnPropertyChanged("TwelveTilePerPage");
            }
        }

        void onPageSwitch(object page)
        {
            DataDispacher.Instance.ChangeData();
            Currentpage = (int)page;
        }

        //Mix the corresponding error data with the structureList.
        void MixErrorWithTile(ref ObservableCollection<CStructureItem> structureItems)
        {
            foreach (CStructureItem cs in structureItems)
            {
                cs.CollectionTypeB = "Red";
                
                cs.Errors = DataDispacher.Instance.LoadFSCErrors("RawZoneId", cs.Id).Count;
                if (cs.Errors == 0)
                {
                    cs.Colors = System.Windows.Media.Color.FromRgb(51, 153, 255).ToString();
                }
                else
                    cs.Colors = System.Windows.Media.Color.FromRgb(255, 51, 51).ToString();
            }
        }

        public ObservableCollection<CStructureItem> TwelveTilePerPage
        {
            get
            {
                ObservableCollection<CStructureItem> TTPP = new ObservableCollection<CStructureItem>();
                if (Currentpage != structureItem.Count / 12 + 1)
                {
                    for (int i = (Currentpage - 1) * 12; i < (Currentpage - 1) * 12 + 12; i++)
                    {
                        if (structureItem[i] != null)
                            TTPP.Add(structureItem[i]);
                    }
                }
                else
                {
                    for (int i = (Currentpage - 1) * 12; i < structureItem.Count; i++)
                    {
                        if (structureItem[i] != null)
                            TTPP.Add(structureItem[i]);
                    }
                }
                MixErrorWithTile(ref TTPP);
                return TTPP;
            }
        }
    }
}
